package com.ling.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ling.bean.Msg;
import com.ling.bean.User;
import com.ling.bean.UserMessage;
import com.ling.service.UserMessageService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.xml.ws.soap.Addressing;
import java.util.List;

/**
 * @author lin
 */
@Controller
public class UserMessageController {
    @Addressing
    private UserMessageService userMessageService;

    public UserMessageController(UserMessageService userMessageService) {
        this.userMessageService = userMessageService;
    }

    //跳转到用户消息中心,默认，公告中心
    @GetMapping("/user-message")
    public String goToUserMessage() {
        return "/user-message";
    }

    //博客消息
    @GetMapping("/user-message-blog")
    public String goToUserMessageBlog() {
        return "/user-message-blog";
    }

    //用户关注
    @GetMapping("/user-message-star")
    public String goToUserMessageStar() {
        return "/user-message-star";
    }

    //问题相关
    @GetMapping("/user-message-question")
    public String goToUserMessageQuestion() {
        return "/user-message-question";
    }

    //回答消息
    @GetMapping("/user-message-answer")
    public String goToUserMessageAnswer() {
        return "/user-message-answer";
    }

    //获取用户消息，分页
    @GetMapping("/messages/{type}/{pn}")
    @ResponseBody
    public Msg getAllUserMessage(@PathVariable Integer type, @PathVariable Integer pn, HttpSession httpSession) {
        User user = (User) httpSession.getAttribute("user");
        if (user == null) {
            return Msg.fail().add("error","请重新登录！");
        }
        //根据不同的类别查询
        PageHelper.startPage(pn, 15);
        List<UserMessage> messages = userMessageService.getAllUserMessageByUserId(user.getId(), type);
        PageInfo<UserMessage> pageInfo = new PageInfo<>(messages);
        return Msg.success().add("pageInfo",pageInfo);
    }

    //消息已读操作
    @GetMapping("/messages/read/{userMessageId}")
    @ResponseBody
    public Msg setHasread(@PathVariable String userMessageId) {
        userMessageService.userMessageSetReadById(userMessageId);
        return Msg.success();
    }

    //消息全部已读
    @GetMapping("/messages/read/type/{type}")
    @ResponseBody
    public Msg setHasreadAll(HttpSession httpSession,@PathVariable Integer type) {
        User user = (User) httpSession.getAttribute("user");
        if (user == null) {
            return Msg.fail().add("error","请重新登录！");
        }
        userMessageService.userMessageSetReadByUserId(user.getId(),type);
        return Msg.success();
    }

    //查看公告
    @GetMapping("/notification-view/{notificationId}")
    public String goToViewNotification(@PathVariable String notificationId, ModelMap modelMap) {
        modelMap.addAttribute("notificationId",notificationId);
        return "/notification-view";
    }
}
